/*
  Copyright 2009 Colin Prinn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package net.recursv.motific.unittest.view;

import net.recursv.motific.unittest.TestMidlet;

import javax.microedition.lcdui.*;

/**
 * Created by: recursv
 * Date: 31/07/2009
 * Time: 9:12:16 PM
 */
public class TestsCanvas extends Canvas implements CommandListener {
    private static final int WHITE = 0xFFFFFF;
    private static final int BLACK = 0x000000;
    private static final int GREY = 0x929292;
    private static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
    private static final int RED = 0xFF0000;
    private static final int GREEN = 0x00FF00;
    private static final int PURPLE = 0x330099;

    private static final String OF = " of ";
    private static final String TEST = "Test: ";
    private final TestMidlet _midlet;
    private final int _numberOfTestCases;
    private final StringBuffer _stringBuffer = new StringBuffer();
    private static final String EXIT = "Exit";
    private static final String INFO = "Details";
    private static final String FAILED = "Failed: ";
    private static final String PASSED = "Passed: ";
    private static final String CASES = "Test Cases: ";
    private static final String SUITES = "Test Suites: ";
    private static final String Motific = "Motific @ recursv.net";

    public TestsCanvas(TestMidlet midlet) {
        _midlet = midlet;
        _numberOfTestCases = _midlet.getNumberOfTestCases();

        addCommand(new Command(EXIT, Command.EXIT, 0));
        addCommand(new Command(INFO, Command.ITEM, 0));
        setCommandListener(this);
    }

    protected void paint(Graphics g) {
        int numberOfCurrentTestCase = _midlet.getNumberOfCurrentTestCase();

        g.setColor(WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(BLACK);
        g.drawString(_midlet.getCurrentTestName(), 20, 20, TOP_LEFT);
        g.drawString(buildTestString(numberOfCurrentTestCase), 20, 80, TOP_LEFT);

        int progressWidth = getWidth() - 40;
        g.fillRoundRect(20, 40, progressWidth, 12, 6, 6);
        if (_numberOfTestCases > 0) {
            g.setColor(_midlet.hasFailedTests() ? RED : GREEN);
            int completeWidth = progressWidth * numberOfCurrentTestCase / _numberOfTestCases;
            g.fillRoundRect(20, 40, completeWidth, 12, 6, 6);
            g.setColor(GREY);
            for (int i = 28; i < completeWidth + 14; i += 8) {
                g.drawLine(i, 40, i, 51);
            }
        }

        g.setColor(BLACK);
        g.drawString(SUITES + _midlet.getNumberOfTestSuites(), 20, 100, TOP_LEFT);
        g.drawString(CASES + _midlet.getNumberOfTestCases(), 20, 120, TOP_LEFT);
        g.setColor(GREEN);
        g.drawString(PASSED + _midlet.getNumberOfSuccessfulTestCases(), 20, 140, TOP_LEFT);
        g.setColor(RED);
        g.drawString(FAILED + _midlet.getNumberOfFailedTestCases(), 20, 160, TOP_LEFT);

        g.setColor(PURPLE);
        g.drawString(Motific, getWidth() - Font.getDefaultFont().stringWidth(Motific) - 10, getHeight() - 5 - Font.getDefaultFont().getHeight(), TOP_LEFT);
    }

    private String buildTestString(int numberOfCurrentTestCase) {
        return _stringBuffer.delete(0, _stringBuffer.length()).append(TEST).append(numberOfCurrentTestCase).append(OF).append(_numberOfTestCases).toString();
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.EXIT) {
            _midlet.notifyDestroyed();
        } else if (command.getCommandType() == Command.ITEM) {
            _midlet.setCanvas(new DetailsCanvas(_midlet));
        }
    }
}
